namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLinePriceTag : PaintLineBase
    {
        private Font wordFont = new Font("New Times Roman", 10f);

        public PaintLinePriceTag()
        {
            base.LineType = PaintLineType.PriceTag;
            base.PlName_zh = "价格签条";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                base.CgInstance.Cursor = Cursors.Hand;
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                int workSpaceX = (int) base.CgInstance.GetWorkSpaceX();
                int workSpaceY = base.CgInstance.GetWorkSpaceY(base.OwnedPanelID);
                if ((workSpaceX > 0) && (workSpaceY > 0))
                {
                    ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                    new Bitmap(workSpaceX, workSpaceY);
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    Brush brush = new SolidBrush(curColor);
                    Pen pen = new Pen(brush) {
                        Width = (float) base.LineWidth
                    };
                    if (base.LineStyle != null)
                    {
                        pen.DashPattern = base.LineStyle;
                    }
                    PaintLineBase.PointValue value2 = pList[0];
                    double chartValue = value2.Value;
                    PaintLineBase.PointValue value3 = pList[0];
                    int pos = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                    float x = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                    float y = (base.CgInstance.GetY(base.OwnedPanelID, chartValue, null) - div.Rect.Y) - div.TitleHeight;
                    base.CgInstance.GetCrossHairPoint();
                    string s = Common.GetValueByDigit(chartValue, (base.CgInstance.LeftYScaleWidth > 0) ? div.LeftYScale.Digit : div.RightYScale.Digit, true);
                    g.DrawString(s, this.wordFont, brush, new PointF(x, y));
                    if (base.Selected)
                    {
                        SizeF ef = g.MeasureString(s, this.wordFont);
                        g.DrawRectangle(pen, x, y, ef.Width, ef.Height);
                    }
                    brush.Dispose();
                    pen.Dispose();
                }
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
                double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
                base.PointList.Clear();
                base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                return true;
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count == 0)
            {
                return false;
            }
            PaintLineBase.PointValue value2 = base.PointList[0];
            double chartValue = value2.Value;
            PaintLineBase.PointValue value3 = base.PointList[0];
            int pos = base.CgInstance.GetRecordByTime(value3.Key) - 1;
            float x = base.CgInstance.GetX(pos);
            float y = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
            Graphics graphics = base.CgInstance.CreateGraphics();
            ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
            Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
            string text = Common.GetValueByDigit(base.CgInstance.GetValueByPoint(crossHairPoint), (base.CgInstance.LeftYScaleWidth > 0) ? div.LeftYScale.Digit : div.RightYScale.Digit, true);
            SizeF ef = graphics.MeasureString(text, this.wordFont);
            RectangleF ef2 = new RectangleF(x, y, ef.Width, ef.Height);
            return ef2.Contains((PointF) crossHairPoint);
        }

        public override double[] LineParam()
        {
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                }
            }
        }

        public Font WordFont
        {
            get
            {
                return this.wordFont;
            }
            set
            {
                this.wordFont = value;
            }
        }
    }
}

